
#####################
####  Study 1   #####
#####################

# This R-Script allows to replicate Figure 1 and Figure 2 in the main text and Table SI-1 and Figure SI-4 in the Appendix.
# The analysis was performed on R Version 4.1.2
# Clear environment #############

rm(list = ls())
gc()



# Installing and Loading Packages #############
#install.packages("dplyr")
#install.packages("srvyr")
#install.packages("ggplot2")
#install.packages("haven")
#install.packages("survey")
#install.packages("vtable")

library(dplyr)
library(srvyr)
library(ggplot2)
library(haven)
library(survey)
library(vtable)
library(base)



# Set Working Directory and Import the dataset ############
setwd("insert here.. /Replication Materials - PSRM")
plotpath<-"insert here.. /Replication Materials - PSRM/Figures/"

## Open-ended responses data (manual analysis) 
Survey1_openendedQ <- readRDS("Data/Survey 1/Survey1_openendedQ.RDS")
## Close-ended responses data 
Survey1_closeendedQ <- readRDS("Data/Survey 1/Survey1_closeendedQ.RDS")

# Generate key variables -----
# female 
Survey1_closeendedQ$female<- factor (with(Survey1_closeendedQ, ifelse ((Survey1_closeendedQ$gender ==2),1,0)))

Survey1_closeendedQ<-Survey1_closeendedQ %>%
  mutate(age_cal=20+birthyear)

Survey1_closeendedQ<-Survey1_closeendedQ%>%
  mutate(age5=case_when(age_cal<25 ~ 1,
                        age_cal>24 & age_cal<35 ~ 2,
                        age_cal>34 & age_cal<45 ~ 3,
                        age_cal>44 & age_cal<55 ~ 4,
                        age_cal>54 ~ 5)) 

Survey1_closeendedQ$age5<-factor(Survey1_closeendedQ$age5)
levels(Survey1_closeendedQ$age5)<- c("18-24","25-34","35-44","45-54","55+")


# low education - Some college and less
Survey1_closeendedQ$lowedu_somecollege<- factor (with(Survey1_closeendedQ, ifelse ((education=="Less than high school" |
                                                                                    education=="High school diploma" | 
                                                                                    education=="Some college" ),1,0)))

# Race 
Survey1_closeendedQ$white<- factor (with(Survey1_closeendedQ, ifelse ((race==7),1,0)))


# ### ----- Main text ----- #### ----
# Figure (1) What Reduction of Inequality Means: Open-ended Responses ############
Survey1_openendedQ<-
  Survey1_openendedQ %>%   
  dplyr:: mutate(barchart_openQ=case_when(
    education==1~"Invest more in public education",
    TaxesSocialservices==1~"Raise taxes on higher earners and \n provides social services for lower earners",
    GeneralPrinciple==1~"A general principle to which \n governments should aspire",
    MinimumWage==1~"Raise the minimum wage",
    taxes_only==1~"Raise taxes on higher earners",
    socialservices_only==1~"Provide social services \n for those with lower incomes",
    unsure==1~"Unsure",
    other==1~"Other"))


Fig1_data<-
  Survey1_openendedQ %>%   
  filter(!is.na(barchart_openQ)) %>%
  dplyr:: filter(!is.na(barchart_openQ)) %>%
  count(barchart_openQ) %>%
  dplyr:: mutate(prop = prop.table(n)) 

Fig_1<-Fig1_data%>%
  mutate(barchart_openQ = fct_reorder(barchart_openQ, desc(prop))) %>%
  ggplot() +
  aes(x = barchart_openQ, fill = barchart_openQ, colour = barchart_openQ, weight = prop) +
  geom_bar(width=0.6) + 
  scale_fill_manual(values = c("#1E7875", "#1E7875", "#CCAB52", "#1E7875", 
                               "#CCAB52","#1E7875", "#CCAB52", "#1E7875"))+   
  scale_colour_manual(values = c("#1E7875", "#1E7875", "#CCAB52", "#1E7875", 
                                 "#CCAB52","#1E7875", "#CCAB52", "#1E7875"))+   
  coord_flip() +      
  scale_y_continuous(labels=scales::percent, limits = c(0, 0.45)) +
  labs(x = " ", y = "") +
  theme_minimal() + theme(legend.position = "none",                     
                          plot.margin = unit(c(0.3, 2, 0.3, 0), "cm"),
                          axis.text=element_text(size=14))

ggsave(Fig_1, dpi = 600,
       filename = paste0(plotpath, "Fig_1.png"),
       height = 5, width = 11, device = "png")
setwd(plotpath)
system("open Fig_1.png")


# Figure (2) What Reduction of Inequality Means: Closed-Ended Responses #############

#Survey1_closeendedQ <- read_dta("insert here - Survey1_closeendedQ.RDS")
Fig_2<-Survey1_closeendedQ%>%
  filter(!is.na(closeQ)) %>%
  mutate(closeQ = fct_infreq(closeQ)) %>%
  ggplot() +
  aes(x = closeQ, fill = closeQ, colour = closeQ) +
  geom_bar(aes(y = (..count..)/sum(..count..)),width=0.5) + 
  coord_flip() +  labs(x = " ", y = "") +
  scale_fill_manual(values = c("#1E7875", "#1E7875", "#1E7875", "#1E7875", "#CCAB52"))+
  scale_colour_manual(values = c("#1E7875", "#1E7875", "#1E7875", "#1E7875", "#CCAB52"))+   
  scale_y_continuous(labels=scales::percent, limits = c(0,0.4))+
  theme_minimal() + theme(legend.position = "none",
                          axis.text = element_text(size=14))
Fig_2

ggsave(Fig_2, dpi = 600,
       filename = paste0(plotpath, "Fig_2.png"),
       height = 5, width = 11, device = "png")
setwd(plotpath)
system("open Fig_2.png")



# ### ----- Appendix ----- #### ----

# Table (SI-1) Summary Statistics: Survey 1 #####
Survey1_descripStat<-Survey1_closeendedQ%>%
  dplyr::select(female, age5, white, education,lowedu_somecollege )

glimpse(Survey1_descripStat)
sumtable(Survey1_descripStat,   
         # out="csv",  file="TableSI_1.csv"
         )


# Figure (SI-4) Distribution of responses for the main outcome question #####
Survey1_closeendedQ$MainQ<- factor(Survey1_closeendedQ$MainQ, levels=c(1,2,3,4), 
                                   labels=c("Definitely \n should",
                                            "Probably \n should",
                                            "Probably \n should not", 
                                            "Definitely \n should not"))

Survey1_closeendedQ$MainQ_support<- factor (with(Survey1_closeendedQ, ifelse ((MainQ<3),1,0)))
levels(Survey1_closeendedQ$MainQ_support)<-c("Oppose","Support")


Fig_SI4<-
  Survey1_closeendedQ %>%
  filter(!is.na(MainQ)) %>%
  ggplot() + aes(x = MainQ) +
  geom_bar(aes(y = (..count..)/sum(..count..)), fill = "#136266", width=0.7) + 
  scale_y_continuous(labels=scales::percent)+
  labs(x = " ", y = "") +
  theme_minimal() +   theme(legend.position = "none",
                            axis.text=element_text(size=12))



ggsave(Fig_SI4, dpi = 600,
       filename = paste0(plotpath, "Fig_SI4.png"),
       height = 5, width = 6, device = "png")
setwd(plotpath)
system("open Fig_SI4.png")
